route.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { type OpenAIListModelResponse } from "@/app/client/platforms/openai";
  2. import { getServerSideConfig } from "@/app/config/server";
  3. import { OpenaiPath } from "@/app/constant";
  4. import { prettyObject } from "@/app/utils/format";
  5. import { NextRequest, NextResponse } from "next/server";
  6. import { auth } from "../../auth";
  7. import { requestOpenai } from "../../common";
  8. const ALLOWD_PATH = new Set(Object.values(OpenaiPath));
  9. function getModels(remoteModelRes: OpenAIListModelResponse) {
  10. const config = getServerSideConfig();
  11. if (config.disableGPT4) {
  12. remoteModelRes.data = remoteModelRes.data.filter(
  13. (m) => !m.id.startsWith("gpt-4"),
  14. );
  15. }
  16. return remoteModelRes;
  17. }
  18. async function handle(
  19. req: NextRequest,
  20. { params }: { params: { path: string[] } },
  21. ) {
  22. console.log("[OpenAI Route] params ", params);
  23. if (req.method === "OPTIONS") {
  24. return NextResponse.json({ body: "OK" }, { status: 200 });
  25. }
  26. const subpath = params.path.join("/");
  27. if (!ALLOWD_PATH.has(subpath)) {
  28. console.log("[OpenAI Route] forbidden path ", subpath);
  29. return NextResponse.json(
  30. {
  31. error: true,
  32. msg: "you are not allowed to request " + subpath,
  33. },
  34. {
  35. status: 403,
  36. },
  37. );
  38. }
  39. const authResult = auth(req);
  40. if (authResult.error) {
  41. return NextResponse.json(authResult, {
  42. status: 401,
  43. });
  44. }
  45. try {
  46. const response = await requestOpenai(req);
  47. // list models
  48. if (subpath === OpenaiPath.ListModelPath && response.status === 200) {
  49. const resJson = (await response.json()) as OpenAIListModelResponse;
  50. const availableModels = getModels(resJson);
  51. return NextResponse.json(availableModels, {
  52. status: response.status,
  53. });
  54. }
  55. return response;
  56. } catch (e) {
  57. console.error("[OpenAI] ", e);
  58. return NextResponse.json(prettyObject(e));
  59. }
  60. }
  61. export const GET = handle;
  62. export const POST = handle;
  63. export const runtime = "edge";
  64. export const preferredRegion = ['arn1', 'bom1', 'cdg1', 'cle1', 'cpt1', 'dub1', 'fra1', 'gru1', 'hnd1', 'iad1', 'icn1', 'kix1', 'lhr1', 'pdx1', 'sfo1', 'sin1', 'syd1'];